package com.example.demo.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description :
 * @Author : Bruce Lee
 * @CreateTime : 2023/11/29
 */


@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private Long id;

    private User user;

    // 存储权限信息
    List<String> permissions = new ArrayList<>();

    public LoginUser(User user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    private List<SimpleGrantedAuthority> authorities = new ArrayList<>();;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(!authorities.isEmpty()){
            return authorities;
        }
        for (String permission : this.permissions) {
            this.authorities.add(new SimpleGrantedAuthority(permission));
        }
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassWord();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    // 账号不存在有效期？true：false
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账号没有被锁？true：false
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 证书不存在有效期？true：false
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 是否生效？true：false
    @Override
    public boolean isEnabled() {
        return true;
    }


}
